home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / site / LWP.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  19.1 KB  |  605 lines

  1.  
  2. package LWP;
  3.  
  4. $VERSION = "5.11";
  5. sub Version { $VERSION; }
  6.  
  7. require 5.004;
  8. require LWP::UserAgent;  # this should load everything you need
  9.  
  10. 1;
  11.  
  12. __END__
  13.  
  14. =head1 NAME
  15.  
  16. LWP - Library for WWW access in Perl
  17.  
  18. =head1 SYNOPSIS
  19.  
  20.   use LWP;
  21.   print "This is libwww-perl-$LWP::VERSION\n";
  22.  
  23.  
  24. =head1 DESCRIPTION
  25.  
  26. Libwww-perl is a collection of Perl modules which provides a simple
  27. and consistent programming interface (API) to the World-Wide Web.  The
  28. main focus of the library is to provide classes and functions that
  29. allow you to write WWW clients, thus libwww-perl said to be a WWW
  30. client library. The library also contain modules that are of more
  31. general use.
  32.  
  33. The main architecture of the library is object oriented.  The user
  34. agent, requests sent and responses received from the WWW server are
  35. all represented by objects.  This makes a simple and powerful
  36. interface to these services.  The interface should be easy to extend
  37. and customize for your needs.
  38.  
  39. The main features of the library are:
  40.  
  41. =over 3
  42.  
  43. =item *
  44.  
  45. Contains various reusable components (modules) that can be
  46. used separately or together.
  47.  
  48. =item *
  49.  
  50. Provides an object oriented model of HTTP-style communication.  Within
  51. this framework we currently support access to http, gopher, ftp, news,
  52. file, and mailto resources.
  53.  
  54. =item *
  55.  
  56. The library be used through the full object oriented interface or
  57. through a very simple procedural interface.
  58.  
  59. =item *
  60.  
  61. Support the basic and digest authorization schemes.
  62.  
  63. =item *
  64.  
  65. Transparent redirect handling.
  66.  
  67. =item *
  68.  
  69. Supports access through proxy servers.
  70.  
  71. =item *
  72.  
  73. URL handling (both absolute and relative URLs are supported).
  74.  
  75. =item *
  76.  
  77. A parser for F<robots.txt> files and a framework for constructing robots.
  78.  
  79. =item *
  80.  
  81. An experimental HTML parser and formatters (for PostScript and plain
  82. text).
  83.  
  84. =item *
  85.  
  86. The library can cooperate with Tk.  A simple Tk-based GUI browser
  87. called 'tkweb' is distributed with the Tk extension for perl.
  88.  
  89. =item *
  90.  
  91. An implementation of the HTTP content negotiation algorithm that can
  92. be used both in protocol modules and in server scripts (like CGI
  93. scripts).
  94.  
  95. =item *
  96.  
  97. A simple command line client application called C<lwp-request>.
  98.  
  99. =back
  100.  
  101.  
  102. =head1 HTTP STYLE COMMUNICATION
  103.  
  104.  
  105. The libwww-perl library is based on HTTP style communication. This
  106. section try to describe what that means.
  107.  
  108. Let us start with this quote from the HTTP specification document
  109. <URL:http://www.w3.org/pub/WWW/Protocols/>:
  110.  
  111. =over 3
  112.  
  113. =item
  114.  
  115. The HTTP protocol is based on a request/response paradigm. A client
  116. establishes a connection with a server and sends a request to the
  117. server in the form of a request method, URI, and protocol version,
  118. followed by a MIME-like message containing request modifiers, client
  119. information, and possible body content. The server responds with a
  120. status line, including the message's protocol version and a success or
  121. error code, followed by a MIME-like message containing server
  122. information, entity meta-information, and possible body content.
  123.  
  124. =back
  125.  
  126. What this means to libwww-perl is that communication always take place
  127. through these steps: First a I<request> object is created and
  128. configured. This object is then passed to a server and we get a
  129. I<response> object in return that we can examine. A request is always
  130. independent of any previous requests, i.e. the service is stateless.
  131. The same simple model is used for any kind of service we want to
  132. access.
  133.  
  134. For example, if we want to fetch a document from a remote file server,
  135. then we send it a request that contains a name for that document and
  136. the response will contain the document itself.  If we access a search
  137. engine, then the content of the request will contain the query
  138. parameters and the response will contain the query result.  If we want
  139. to send a mail message to somebody then we send a request object which
  140. contains our message to the mail server and the response object will
  141. contain an acknowledgment that tells us that the message has been
  142. accepted and will be forwarded to the recipient(s).
  143.  
  144. It is as simple as that!
  145.  
  146.  
  147. =head2 The Request Object
  148.  
  149. The request object has the class name C<HTTP::Request> in
  150. libwww-perl. The fact that the class name use C<HTTP::> as a name
  151. prefix only implies that we use the HTTP model of communication. It
  152. does not limit the kind of services we can try to pass this I<request>
  153. to.  For instance, we will send C<HTTP::Request>s both to ftp and
  154. gopher servers, as well as to the local file system.
  155.  
  156. The main attributes of the request objects are:
  157.  
  158. =over 3
  159.  
  160. =item *
  161.  
  162. The B<method> is a short string that tells what kind of
  163. request this is.  The most used methods are B<GET>, B<PUT>,
  164. B<POST> and B<HEAD>.
  165.  
  166. =item *
  167.  
  168. The B<url> is a string denoting the protocol, server and
  169. the name of the "document" we want to access.  The B<url> might
  170. also encode various other parameters.
  171.  
  172. =item *
  173.  
  174. The B<headers> contain additional information about the
  175. request and can also used to describe the content.  The headers
  176. is a set of keyword/value pairs.
  177.  
  178. =item *
  179.  
  180. The B<content> is an arbitrary amount of data.
  181.  
  182. =back
  183.  
  184. =head2 The Response Object
  185.  
  186. The request object has the class name C<HTTP::Response> in
  187. libwww-perl.  The main attributes of objects of this class are:
  188.  
  189. =over 3
  190.  
  191. =item *
  192.  
  193. The B<code> is a numerical value that encode the overall
  194. outcome of the request.
  195.  
  196. =item *
  197.  
  198. The B<message> is a short (human readable) string that
  199. corresponds to the I<code>.
  200.  
  201. =item *
  202.  
  203. The B<headers> contain additional information about the
  204. response and they also describe the content.
  205.  
  206. =item *
  207.  
  208. The B<content> is an arbitrary amount of data.
  209.  
  210. =back
  211.  
  212. Since we don't want to handle all possible I<code> values directly in
  213. our programs, the libwww-perl response object have methods that can be
  214. used to query what kind of response this is.  The most commonly used
  215. response classification methods are:
  216.  
  217. =over 3
  218.  
  219. =item is_success()
  220.  
  221. The request was was successfully received, understood or accepted.
  222.  
  223. =item is_error()
  224.  
  225. The request failed.  The server or the resource might not be
  226. available, access to the resource might be denied or other things might
  227. have failed for some reason.
  228.  
  229. =back
  230.  
  231. =head2 The User Agent
  232.  
  233. Let us assume that we have created a I<request> object. What do we
  234. actually do with it in order to receive a I<response>?
  235.  
  236. The answer is that you pass it on to a I<user agent> object and this
  237. object will take care of all the things that need to be done
  238. (low-level communication and error handling). The user agent will give
  239. you back a I<response> object. The user agent represents your
  240. application on the network and it provides you with an interface that
  241. can accept I<requests> and will return I<responses>.
  242.  
  243. You should think about the user agent as an interface layer between
  244. your application code and the network.  Through this interface you are
  245. able to access the various servers on the network.
  246.  
  247. The libwww-perl class name for the user agent is
  248. C<LWP::UserAgent>. Every libwww-perl application that wants to
  249. communicate should create at least one object of this kind. The main
  250. method provided by this object is request(). This method takes an
  251. C<HTTP::Request> object as argument and will (eventually) return a
  252. C<HTTP::Response> object.
  253.  
  254. The user agent has many other attributes that lets you
  255. configure how it will interact with the network and with your
  256. application code.
  257.  
  258. =over 3
  259.  
  260. =item *
  261.  
  262. The B<timeout> specify how much time we give remote servers in
  263. creating responses before the library disconnect and creates an
  264. internal I<timeout> response.
  265.  
  266. =item *
  267.  
  268. The B<agent> specify the name that your application should use when it
  269. presents itself on the network.
  270.  
  271. =item *
  272.  
  273. The B<from> attribute can be set to the e-mail address of the person
  274. responsible for running the application.  If this is set, then the
  275. address will be sent to the servers with every request.
  276.  
  277. =item *
  278.  
  279. The B<use_alarm> specify if it is OK for the user agent to use the
  280. alarm(2) system to implement timeouts.
  281.  
  282. =item *
  283.  
  284. The B<use_eval> specify if the agent should raise an
  285. exception (C<die> in perl) if an error condition occur.
  286.  
  287. =item *
  288.  
  289. The B<parse_head> specify whether we should initialize response
  290. headers from the E<lt>head> section of HTML documents.
  291.  
  292. =item *
  293.  
  294. The B<proxy> and B<no_proxy> specify if and when communication should
  295. go through a proxy server. <URL:http://www.w3.org/pub/WWW/Proxies/>
  296.  
  297. =item *
  298.  
  299. The B<credentials> provide a way to set up user names and
  300. passwords that is needed to access certain services.
  301.  
  302. =back
  303.  
  304. Many applications would want even more control over how they interact
  305. with the network and they get this by specializing the
  306. C<LWP::UserAgent> by sub-classing.  The library provide a
  307. specialization called C<LWP::RobotUA> that is used by robot
  308. applications.
  309.  
  310. =head2 An Example
  311.  
  312. This example shows how the user agent, a request and a response are
  313. represented in actual perl code:
  314.  
  315.   use LWP::UserAgent;
  316.   $ua = new LWP::UserAgent;
  317.   $ua->agent("AgentName/0.1 " . $ua->agent);
  318.  
  319.   my $req = new HTTP::Request POST => 'http://www.perl.com/cgi-bin/BugGlimpse';
  320.   $req->content_type('application/x-www-form-urlencoded');
  321.   $req->content('match=www&errors=0');
  322.  
  323.   my $res = $ua->request($req);
  324.  
  325.   if ($res->is_success) {
  326.       print $res->content;
  327.   } else {
  328.       print "Bad luck this time\n";
  329.   }
  330.  
  331. The $ua is created once when the application starts up.  New request
  332. objects are normally created for each request sent.
  333.  
  334.  
  335. =head1 NETWORK SUPPORT
  336.  
  337. This section goes through the various protocol schemes and describe
  338. the HTTP style methods that are supported and the headers that might
  339. have any effect.
  340.  
  341. For all requests, a "User-Agent" header is added and initialized from
  342. the $ua->agent value before the request is handed to the network
  343. layer.  In the same way, a "From" header is initialized from the
  344. $ua->from value.
  345.  
  346. For all responses, the library will add a header called "Client-Date".
  347. This header will encode the time when the response was received by
  348. your application.  This format and semantics of the header is just
  349. like the server created "Date" header.
  350.  
  351. =head2 HTTP Requests
  352.  
  353. HTTP request are really just handed off to an HTTP server and it will
  354. decide what happens.  Few servers implement methods beside the usual
  355. "GET", "HEAD", "POST" and "PUT" but CGI-scripts can really implement
  356. any method they like.
  357.  
  358. If the server is not available then the library will generate an
  359. internal error response.
  360.  
  361. The library automatically adds a "Host" and a "Content-Length" header
  362. to the HTTP request before it is sent over the network.
  363.  
  364. For GET request you might want to add the "If-Modified-Since" header
  365. to make the request conditional.
  366.  
  367. For POST request you should add the "Content-Type" header.  When you
  368. try to emulate HTML E<lt>FORM> handling you should usually let the value
  369. of the "Content-Type" header be "application/x-www-form-urlencoded".
  370. See L<lwpcook> for examples of this.
  371.  
  372. The libwww-perl HTTP implementation currently support the HTTP/1.0
  373. protocol.  HTTP/0.9 servers are also handled correctly.
  374.  
  375. The library allows you to access proxy server through HTTP.  This
  376. means that you can set up the library to forward all types of request
  377. through the HTTP protocol module.  See L<LWP::UserAgent> for
  378. documentation of this.
  379.  
  380.  
  381.  
  382.  
  383. =head2 FTP Requests
  384.  
  385. The library currently support GET, HEAD and PUT requests.  GET will
  386. retrieve a file or a directory listing from an FTP server.  PUT will
  387. store a file on a ftp server.
  388.  
  389. You can specify a ftp account for servers that want this in addition
  390. user name and password.  This is specified by passing an "Account"
  391. header in the request.
  392.  
  393. User name/password can be specified using basic authorization or be
  394. encoded in the URL.  Bad logins return an UNAUTHORIZED response with
  395. "WWW-Authenticate: Basic" and can be treated as basic authorization
  396. for HTTP.
  397.  
  398. The library support ftp ASCII transfer mode by specifying the "type=a"
  399. parameter in the URL.
  400.  
  401. Directory listings are by default returned unprocessed (as returned
  402. from the ftp server) with the content media type reported to be
  403. "text/ftp-dir-listing". The C<File::Listing> module provide functionality
  404. for parsing of these directory listing.
  405.  
  406. The ftp module is also able to convert directory listings to HTML and
  407. this can be requested via the standard HTTP content negotiation
  408. mechanisms (add an "Accept: text/html" header in the request if you
  409. want this).
  410.  
  411. The normal file retrievals, the "Content-Type" is guessed based on the
  412. file name suffix. See L<LWP::MediaTypes>.
  413.  
  414. The "If-Modified-Since" header is not honored yet.
  415.  
  416. Example:
  417.  
  418.   $req = HTTP::Request->new(GET => 'ftp://me:passwd@ftp.some.where.com/');
  419.   $req->header(Accept => "text/html, */*;q=0.1");
  420.  
  421. =head2 News Requests
  422.  
  423. Access to the USENET News system is implemented through the NNTP
  424. protocol.  The name of the news server is obtained from the
  425. NNTP_SERVER environment variable and defaults to "news".  It is not
  426. possible to specify the hostname of the NNTP server in the news:-URLs.
  427.  
  428. The library support GET and HEAD to retrieve news articles through the
  429. NNTP protocol.  You can also post articles to newsgroups by using
  430. (surprise!) the POST method.
  431.  
  432. GET on newsgroups is not implemented yet.
  433.  
  434. Examples:
  435.  
  436.   $req = HTTP::Request->new(GET => 'news:abc1234@a.sn.no');
  437.  
  438.   $req = HTTP::Request->new(POST => 'news:comp.lang.perl.test');
  439.   $req->header(Subject => 'This is a test',
  440.                From    => 'me@some.where.org');
  441.   $req->content(<<EOT);
  442.   This is the content of the message that we are sending to
  443.   the world.
  444.   EOT
  445.  
  446.  
  447. =head2 Gopher Request
  448.  
  449. The library supports the GET and HEAD method for gopher request.  All
  450. request header values are ignored.  HEAD cheats and will return a
  451. response without even talking to server.
  452.  
  453. Gopher menus are always converted to HTML.
  454.  
  455. The response "Content-Type" is generated from the document type
  456. encoded (as the first letter) in the request URL path itself.
  457.  
  458. Example:
  459.  
  460.   $req = HTTP::Request->new('GET', 'gopher://gopher.sn.no/');
  461.  
  462.  
  463.  
  464. =head2 File Request
  465.  
  466. The library supports GET and HEAD methods for file requests.  The
  467. "If-Modified-Since" header is supported.  All other headers are
  468. ignored.  The I<host> component of the file URL must be empty or set
  469. to "localhost".  Any other I<host> value will be treated as an error.
  470.  
  471. Directories are always converted to an HTML document.  For normal
  472. files, the "Content-Type" and "Content-Encoding" in the response are
  473. guessed based on the file suffix.
  474.  
  475. Example:
  476.  
  477.   $req = HTTP::Request->new(GET => 'file:/etc/passwd');
  478.  
  479.  
  480. =head2 Mailto Request
  481.  
  482. You can send (aka "POST") mail messages using the library.  All
  483. headers specified for the request are passed on to the mail system.
  484. The "To" header is initialized from the mail address in the URL.
  485.  
  486. Example:
  487.  
  488.   $req = HTTP::Request->new(POST => 'mailto:libwww-perl-request@ics.uci.edu');
  489.   $req->header("Subject", "subscribe");
  490.   $req->content("Please subscribe me to the libwww-perl mailing list!\n");
  491.  
  492.  
  493. =head1 OVERVIEW OF CLASSES AND PACKAGES
  494.  
  495. This table should give you a quick overview of the classes provided by the
  496. library. Indentation shows class inheritance.
  497.  
  498.  LWP::MemberMixin   -- Access to member variables of Perl5 classes
  499.    LWP::UserAgent   -- WWW user agent class
  500.      LWP::RobotUA   -- When developing a robot applications
  501.    LWP::Protocol          -- Interface to various protocol schemes
  502.      LWP::Protocol::http  -- http:// access
  503.      LWP::Protocol::file  -- file:// access
  504.      LWP::Protocol::ftp   -- ftp:// access
  505.      ...
  506.  
  507.  LWP::Socket        -- Socket creation and IO
  508.  
  509.  HTTP::Headers      -- MIME/RFC822 style header (used by HTTP::Message)
  510.  HTTP::Message      -- HTTP style message
  511.    HTTP::Request    -- HTTP request
  512.    HTTP::Response   -- HTTP response
  513.  HTTP::Daemon       -- A HTTP server class
  514.  
  515.  URI::URL           -- Uniform Resource Locators
  516.  
  517.  WWW::RobotRules    -- Parse robots.txt files
  518.    WWW::RobotRules::AnyDBM_File -- Persistent RobotRules
  519.  
  520.  HTML::Parser       -- Parse HTML documents
  521.    HTML::TreeBuilder-- Build a HTML syntax tree
  522.    HTML::HeadParser -- Parse the <HEAD> section of a HTML document
  523.    HTML::LinkExtor  -- Extract links from a HTML document
  524.  HTML::Element      -- Building block for the HTML::TreeBuilder
  525.  HTML::Formatter    -- Convert HTML syntax trees to readable formats
  526.    HTML::FormatText -- Output is plain text
  527.    HTML::FormatPS   -- Output is PostScript
  528.  
  529. The following modules provide various functions and definitions.
  530.  
  531.  LWP                -- This file.  Library version number and documentation.
  532.  LWP::MediaTypes    -- MIME types configuration (text/html etc.)
  533.  LWP::Debug         -- Debug logging module
  534.  LWP::Simple        -- Simplified procedural interface for common functions
  535.  HTTP::Status       -- HTTP status code (200 OK etc)
  536.  HTTP::Date         -- Date parsing module for HTTP date formats
  537.  HTTP::Negotiate    -- HTTP content negotiation calculation
  538.  HTML::Entities     -- Expand or unexpand entities in HTML text
  539.  File::Listing      -- Parse directory listings
  540.  
  541. HTTP use the Base64 encoding at some places.  The QuotedPrint module
  542. is just included to make the MIME:: collection more complete.
  543.  
  544.  MIME::Base64       -- Base64 encoding/decoding routines
  545.  MIME::QuotedPrint  -- Quoted Printable encoding/decoding routines
  546.  
  547. The following modules does not have much to do with the World Wide
  548. Web, but are included just because I am lazy and did not bother to
  549. make separate distributions for them.  Regard them as bonus, provided
  550. free for your pleasure.
  551.  
  552.  Font::AFM          -- Parse Adobe Font Metric files
  553.  File::CounterFile  -- Persistent counter class
  554.  
  555.  
  556. =head1 MORE DOCUMENTATION
  557.  
  558. All modules contain detailed information on the interfaces they
  559. provide.  The L<lwpcook> is the libwww-perl cookbook that contain
  560. examples of typical usage of the library.  You might want to take a
  561. look at how the scripts C<lwp-request>, C<lwp-rget> and C<lwp-mirror>
  562. are implemented.
  563.  
  564. =head1 BUGS
  565.  
  566. The library can not handle multiple simultaneous requests yet.  The
  567. HTML:: modules are still experimental.  Also, check out what's left in
  568. the TODO file.
  569.  
  570. =head1 ACKNOWLEDGEMENTS
  571.  
  572. This package owes a lot in motivation, design, and code, to the
  573. libwww-perl library for Perl 4, maintained by Roy Fielding
  574. E<lt>fielding@ics.uci.edu>.
  575.  
  576. That package used work from Alberto Accomazzi, James Casey, Brooks
  577. Cutter, Martijn Koster, Oscar Nierstrasz, Mel Melchner, Gertjan van
  578. Oosten, Jared Rhine, Jack Shirazi, Gene Spafford, Marc VanHeyningen,
  579. Steven E. Brenner, Marion Hakanson, Waldemar Kebsch, Tony Sanders, and
  580. Larry Wall; see the libwww-perl-0.40 library for details.
  581.  
  582. The primary architect for this Perl 5 library is Martijn Koster and
  583. Gisle Aas, with lots of help from Graham Barr, Tim Bunce, Andreas
  584. Koenig, Jared Rhine, and Jack Shirazi.
  585.  
  586.  
  587. =head1 COPYRIGHT
  588.  
  589.   Copyright 1995-1997, Gisle Aas
  590.   Copyright 1995, Martijn Koster
  591.  
  592. This library is free software; you can redistribute it and/or
  593. modify it under the same terms as Perl itself.
  594.  
  595. =head1 AVAILABILITY
  596.  
  597. The latest version of this library is likely to be available from:
  598.  
  599.  http://www.sn.no/libwww-perl/
  600.  
  601. The best place to discuss this code is on the
  602. <libwww-perl@ics.uci.edu> mailing list.
  603.  
  604. =cut
  605.